package edu.whut.method;

import java.util.Random;
import java.util.Scanner;

/**
 * 随机产生验证码
 */
public class test04 {
    public static void main(String[] args) {
        char[] chs = getContent();//创建验证码数组
        String checkCode = getCheckCode(chs);//产生验证码
        checkInCode(checkCode);//验证
    }

    private static void checkInCode(String checkCode) {
        System.out.println("请输入验证码：");
        Scanner scanner = new Scanner(System.in);
        String inputCheckCode = scanner.next();
        if(checkCode.equals(inputCheckCode)){
            System.out.println("验证成功！");
        }else{
            System.out.println("验证码错误！");
        }
    }

    private static String getCheckCode(char[] chs) {
        String checkCode = "";
        Random r = new Random();
        for (int i = 0; i < 4; i++) {
            int x = r.nextInt(chs.length);
            checkCode += chs[x];
        }
        System.out.println(checkCode);
        return checkCode;
    }

    private static char[] getContent() {
        char[] chs = new char[26+26+10];
        int k=0;
        for(char c = 'a';c<='z';c++){
            chs[k++]=c;
        }
        for(char c = 'A';c<='Z';c++){
            chs[k++]=c;
        }
        for(char c='0';c<='9';c++){
            chs[k++]=c;
        }
        //打印字符类型数组名，直接看到内容
        // System.out.println(chs);
        return chs;
    }
}
